<?php
/**
 * 系统公告
 *
 */
class Controller_Admin_Content extends Controller_template
{

	public function __construct ($req, $res)
	{
		$this->template = 'smarty:admin/notice/list';
		parent::__construct ($req, $res);
	}
    /**
	 * 公告入口
	 *
	 */
	public function action_index()
	{
		$cities = user::cities();
		$city_info = current($cities);
		$city      = $city_info['id'];
		
		$url       = '/content/notice_lists/'.$city;
		$this->request->redirect($url);
	}
	/**
	 * 公告显示入口
	 */
	public function action_notice_lists($city)
	{
       $cities = user::cities();
	   $city_find = "";
	   $city_id   = 0;
	   if($city != 0)
	   {
	   foreach($cities as $value)
		{
		      if($value['id']==$city)
			  {
			     $city_find = $city;
				 break;
			  }
		}

	    // 循环后重置指针
	    reset($cities);
       if($city_find != '')
	   {
		   $city_id = $city;
	    }
		else
		{
		    $city_current = current($cities);
			$city_id      = $city_current['id'];
			$url          = "/content/notice_lists/".$city_id;
			$this->request->redirect($url);
		 }
       }
       $notices  = Notice::factory('sysnotice')->lists($city_id);
	   $this->template->set('city_id',$city_id);
	   $this->template->set('citys',$cities);
	   $this->template->set('notices',$notices);
	   $this->template->set('function_name','snotice');
	}
    /**
	 *  公告添加
	 *  @param $city_id 城市id
	 */
     function action_notice_add($city_id=0)
	 { 
       $title    = $this->request->post('title');
	   $content  = $this->request->post('notice_content');
	   $content  = strip_tags($content);
	   $content  = htmlspecialchars($content);
	   $notices  = Notice::factory('sysnotice')->lists($city_id);

       $num  = count($notices);
	   // 每个城市公告仅为2条
	   if($num <2)
	   {
			if($title)
			{
			Notice::factory('sysnotice')->title($title)->message($content)->send($city_id);
			$url  = '/content/notice_lists/'.$city_id;
			$this->request->redirect($url);
			}
			else
			{
			$tpl  = 'smarty:admin/notice/add';
	        $this->template->set('function_name','snotice');
			$this->template->set_filename($tpl);
			}
	   }
	   else
	   {    
			$url  = '/content/notice_lists/'.$city_id;
			$this->request->redirect($url);
	   }
	 }

	 /**
	  *  编辑公告
	  *  @param $notice_id 公告id
	  */
      function action_notice_edit($notice_id)
	  {
			$title   = $this->request->post('title');
			$content = $this->request->post('notice_content');
			$content  = strip_tags($content);
			$content = htmlspecialchars($content);
			$notices = Notice::factory('sysnotice')->detail($notice_id);
			$city_id = $notices['cityid'];

	   if($title)
	   {
			Notice::factory('sysnotice')->title($title)->message($content)->to($notice_id)->send($city_id);
	        $url = '/content/notice_lists/'.$city_id;
	        $this->request->redirect($url);
	   }
	   else
	   {
	        $tpl = 'smarty:admin/notice/edit';
		    $this->template->set('notices',$notices);
	        $this->template->set('function_name','snotice');
		    $this->template->set_filename($tpl);
	   }
	  }
	  
	  /**
	   *  删除公告
	   *  @param $notice_id 公告id
	   */
	  function action_notice_del($notice_id)
	  {
	  		Notice::factory('sysnotice')->de($notice_id)->send($notice_id);
	  		// 获取来源页的网址
	  		$url        = $_SERVER['HTTP_REFERER'];
	  		if(!$url)
	  		{
	  			$url    = '/content/index';
	  		}
	  		
	  		$this->request->redirect($url);
           
	  }
}
